package com.cloudcc.mobile.view.customer;

import java.util.List;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.ui.viewgroup.loadmore.LoadMoreContainer;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.map.MapLocation;
import com.cloudcc.mobile.presenter.CustomerPresenter;
import com.cloudcc.mobile.view.main.fragment.BaseListFragment;
import com.cloudcc.mobile.widget.listview.CloudCCListView;
import com.cloudcc.mobile.widget.listview.CloudCCListView.OnRefreshOrLoadMoreListener;

/**
 * 潜在客户 联系人 客户 的基类
 * 
 * @Description: TODO()
 * @author wangzhiqiang
 * @date 2016-3-10 下午2:31:00
 * 
 */
public abstract class BaseCustomerListFragment<T> extends BaseListFragment
		implements OnRefreshOrLoadMoreListener, OnItemClickListener {

	@Bind(R.id.listview1)
	CloudCCListView mListView;

	@Bind(R.id.et_search_content)
	EditText mSearchContent;

	protected CommonAdapter<T> mAdapter;
	private boolean isSearch = false;
	private boolean isFristVisible = true;
    /** 是否已被加载过一次，第二次就不再去请求数据了 */
    private boolean mHasLoadedOnce;
	private CustomerPresenter mCustomerPresenter = new CustomerPresenter();
	protected MapLocation location;

	@Override
	public void init() {
		super.init();
		mListView.setOnRefreshOrLoadMoreListener(this);
		initEditText();
		location = (MapLocation) getActivity().getIntent()
				.getSerializableExtra("MapsActivity");
	}

	protected boolean isVisible;
	
	public void onNowResume(){
		lazyLoad();
	}


	/**
	 * 延迟加载 子类必须重写此方法
	 */
	protected void lazyLoad(){
		if (mHasLoadedOnce) {
			return;
		}
		mListView.requestRefresh();
		mHasLoadedOnce =true;
	}

	private void initEditText() {
		mSearchContent
				.setOnEditorActionListener(new EditText.OnEditorActionListener() {

					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_SEARCH) {
							InputMethodManager imm = (InputMethodManager) v
									.getContext().getSystemService(
											Context.INPUT_METHOD_SERVICE);
							imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
							clickSearch();
							return true;
						}
						return false;
					}

				});
	}

	@OnClick(R.id.search)
	public void clickSearch() {
		isSearch = true;
		mListView.requestRefresh();
	}

	protected void handlerOnDataReqestFinish(boolean shoudClear, List<T> data) {
		if (mAdapter == null) {
			mAdapter = createNewAdapter();
			mListView.setAdapter(mAdapter);
			mListView.setOnItemClickListener(this);
		}
		if (shoudClear) {
			mAdapter.changeData(data);
		} else {
			mAdapter.addData(data);
		}
	}

	@Override
	public void requestData() {
		mCustomerPresenter.queryCustomerListByType(currentPage, isSearch,
				getObjApiName(), mSearchContent.getText().toString().trim());
	}

	@Override
	public void onRefresh(PtrFrameLayout frame) {
		onRefreshData();
	}

	@Override
	public void onLoadMore(LoadMoreContainer frame) {
		currentPage++;
		requestData();
	}

	@Override
	public int getLayoutId() {
		return R.layout.qzkh_list_frag;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		T t = mAdapter.getItem(position);

		onItemClick(position, t);
	}

	protected void onItemClick(int position, T t) {

	}

	/**
	 * 创建adapter
	 * 
	 * @return
	 */
	protected abstract CommonAdapter<T> createNewAdapter();

	protected abstract String getObjApiName();

}
